$(document).ready(function(){
    var formData = 0;
    $('.check_data').each(function(){
        $(this).on('blur',function(){
            dataCheck(removeAllSpace($(this).val()),$(this).attr('data-type'),$(this));
        });
        $(this).parent().css('position','relative').append("<p class='data_message' style='display:none; color:red;'></p>");
    })

    function removeAllSpace(str) {
      return str.replace(/\s+/g, "");
    }

    function dataCheck(val,dataType,This){
        if (val == "") {
            formData = 1;
            emptyStr = This.attr('data-tips') ? This.attr('data-tips') : "该栏目不能为空";
            This.siblings('.data_message').html(emptyStr).fadeIn();
            return;
        }else{
            This.siblings('.data_message').fadeOut();
        }
        switch (dataType){
            case "email":
            var isEmail = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/;
            if (!isEmail.test(val)) {
                formData = 1;
                This.siblings('.data_message').html("请输入正确的邮箱格式！").fadeIn();
                return;
            }else{
                formData = 0;
                This.siblings('.data_message').fadeOut();
            }
            break;

            case "num":
            var isNum = /^[0-9]*$/;
            if (!isNum.test(val)) {
                formData = 1;
                This.siblings('.data_message').html("请输入数字！").fadeIn();
                return;
            }else{
                formData = 0;
                This.siblings('.data_message').fadeOut();
            }
            break;

            case "notnum":
            var isNum = /^[0-9]*$/;
            if (isNum.test(val)) {
                formData = 1;
                This.siblings('.data_message').html("不能为纯数字！").fadeIn();
                return;
            }else{
                formData = 0;
                This.siblings('.data_message').fadeOut();
            }
            break;

            case "mobile":
            var isMobile = /^1[0-9]{10}$/;
            if (!isMobile.test(val)) {
                formData = 1;
                This.siblings('.data_message').html("请填写正确的手机号码！").fadeIn();
                return;
            }else{
                formData = 0;
                This.siblings('.data_message').fadeOut();
            }
            break;

            default:
            break;
        }
    }

    $('#form_submit').on('click',function(){
        $('.check_data').each(function(){
            dataCheck(removeAllSpace($(this).val()),$(this).attr('data-type'),$(this));
        })
        if(formData)return false;
    })
})